﻿Public Class Dcorredora

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of Ecorredora)
        Dim corredoras As List(Of Ecorredora) = Nothing 'Es una coleccion de lista tipado (vendedor)

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_corredora_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            corredoras = New List(Of Ecorredora)
                            Dim corredora As Ecorredora = Nothing 'tipo 
                            Do While dr.Read
                                corredora = New Ecorredora( _
                                    dr.GetInt32(dr.GetOrdinal("idCorredora")), _
                                    dr.GetString(dr.GetOrdinal("nombreCorredora")), _
                                    dr.GetString(dr.GetOrdinal("apeMatCorredora")), _
                                    dr.GetString(dr.GetOrdinal("apePatCorredora")), _
                                    dr.GetString(dr.GetOrdinal("emailCorredora")), _
                                    dr.GetString(dr.GetOrdinal("telefonoCorredora")), _
                                    dr.GetString(dr.GetOrdinal("celularCorredora")), _
                                    dr.GetString(dr.GetOrdinal("direccionCorredora")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                corredoras.Add(corredora)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return corredoras
    End Function
    Public Shared Function SeleccionarXEstado(ByVal descEmpresa As String) As List(Of Ecorredora)
        Dim corredoras As List(Of Ecorredora) = Nothing 'Es una coleccion de lista tipado (vendedor)

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_corredora_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            corredoras = New List(Of Ecorredora)
                            Dim corredora As Ecorredora = Nothing 'tipo 
                            Do While dr.Read
                                corredora = New Ecorredora( _
                                    dr.GetInt32(dr.GetOrdinal("idCorredora")), _
                                    dr.GetString(dr.GetOrdinal("nombreCorredora")), _
                                    dr.GetString(dr.GetOrdinal("apeMatCorredora")), _
                                    dr.GetString(dr.GetOrdinal("apePatCorredora")), _
                                    dr.GetString(dr.GetOrdinal("emailCorredora")), _
                                    dr.GetString(dr.GetOrdinal("telefonoCorredora")), _
                                    dr.GetString(dr.GetOrdinal("celularCorredora")), _
                                    dr.GetString(dr.GetOrdinal("direccionCorredora")), _
                                    dr.GetString(dr.GetOrdinal("estado")), _
                                    dr.GetInt32(dr.GetOrdinal("idUsuario")))
                                corredoras.Add(corredora)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return corredoras
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal corredora As Ecorredora) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_corredora_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@nombreCorredora", SqlDbType.VarChar, 55).Value = corredora.nombreCorredora
                cmd.Parameters.Add("@apeMatCorredora", SqlDbType.VarChar, 55).Value = corredora.apeMatCorredora
                cmd.Parameters.Add("@apePatCorredora", SqlDbType.VarChar, 55).Value = corredora.apePatCorredora
                cmd.Parameters.Add("@emailCorredora", SqlDbType.VarChar, 55).Value = corredora.emailCorredora
                cmd.Parameters.Add("@telefonoCorredora", SqlDbType.VarChar, 55).Value = corredora.telefonoCorredora
                cmd.Parameters.Add("@celularCorredora", SqlDbType.VarChar, 55).Value = corredora.celularCorredora
                cmd.Parameters.Add("@direccionCorredora", SqlDbType.VarChar, 55).Value = corredora.direccionCorredora
                'cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = ent.nombreCorredora
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = corredora.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal corredora As Ecorredora) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_corredora_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idCorredora", SqlDbType.Int).Value = corredora.idCorredora
                cmd.Parameters.Add("@nombreCorredora", SqlDbType.VarChar, 55).Value = corredora.nombreCorredora
                cmd.Parameters.Add("@apeMatCorredora", SqlDbType.VarChar, 55).Value = corredora.apeMatCorredora
                cmd.Parameters.Add("@apePatCorredora", SqlDbType.VarChar, 55).Value = corredora.apePatCorredora
                cmd.Parameters.Add("@emailCorredora", SqlDbType.VarChar, 55).Value = corredora.emailCorredora
                cmd.Parameters.Add("@telefonoCorredora", SqlDbType.VarChar, 55).Value = corredora.telefonoCorredora
                cmd.Parameters.Add("@celularCorredora", SqlDbType.VarChar, 55).Value = corredora.celularCorredora
                cmd.Parameters.Add("@direccionCorredora", SqlDbType.VarChar, 55).Value = corredora.direccionCorredora
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = corredora.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = corredora.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal corredora As Ecorredora) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_corredora_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idCorredora", SqlDbType.Int).Value = corredora.idCorredora
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = corredora.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
